﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HomeWork4_2
{
    public delegate void ShowTime(Object sender,Time time);
    public class Time
    {
        public int H { get; set; }
        public int M { get; set; }
        public  int S { get; set; }
    }
    //闹钟类
    public class AlarmClock
    {
        public event ShowTime OnShow;
        //闹钟时间
        Time alarmClock=new Time ();
        public AlarmClock(int h,int m,int s)
        {
            alarmClock.H = h;
            alarmClock.M = m;
            alarmClock.S = s;
        }
        //闹钟开始运行
        public void GoNow()
        {
            while (true)
            {
                alarmClock.S ++;
                if (alarmClock.S >= 60)
                {
                    alarmClock.S = 0;
                    alarmClock.M++;
                    if(alarmClock.M >= 60)
                    {
                        alarmClock.M = 0;
                        alarmClock.H++;
                    }
                }
                OnShow(this, alarmClock);
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
    public class RunAlarm
    {
        //响铃时间
        public Time alarmTime = new Time();
        public AlarmClock alarmClock;
        public RunAlarm(int nowHour,int nowMin,int nowSec,int alarmHour,int alarmMin,int alarmSec)
        {
            //设置闹钟开始运行时间
            alarmClock = new AlarmClock(nowHour ,nowMin ,nowSec);
            alarmTime.H = alarmHour;
            alarmTime.M = alarmMin;
            alarmTime.S = alarmSec;
            alarmClock.OnShow += Tick;
            alarmClock.OnShow += Alarm;
        }
        public void Tick(Object sender, Time time)
        {
            Console.WriteLine($"{time.H}:{time.M }:{time.S }");
        }
        public void Alarm(Object sender, Time time)
        {
            //检测闹铃时间是否与响铃时间相同
            if(time.H==alarmTime.H&& time.M  == alarmTime.M && time.S == alarmTime.S )
            Console.WriteLine("Ring~Ring~Ring...");
        }

    }
    class Program
    {
      
        static void Main(string[] args)
        {
            RunAlarm runAlarm = new RunAlarm(1,15,50,1,15,59);
            runAlarm.alarmClock.GoNow();

        }
    }
}
